<?php if ( !defined('BASEPATH')) exit('No direct script access allowed');

class Abstract_report extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');
		$this->load->library('PHPexcel');
		$this->load->helper('download');
		$this->load->helper('file');

	}
	public function index(){
	  $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  $this->load->view($this->session->userdata('formain'));
	  $this->load->view('layouts/footer');
	 }

	public function get_status_abs(){
	 	$th_from   = $this->input->post('rn_from');
	 	$th_to     = $this->input->post('rn_to');

	 $result = $this->abstract_report_model->get_status($th_from,$th_to);
		echo json_encode($result);		
	 }

	 public function gen_pdf() {
		$result = $this->abstract_report_model->get_status($_GET['rn_from'],$_GET['rn_to']);
		//var_dump($result);
		$table_data = '';
		//<td style='border-bottom:1px solid; text-align: center;width=8%; padding: 5px;'>Year</td>
		//<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->year . '</td>
		foreach ($result as $key) {
			$table_data .= '<tr><td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;">' . $key->receipt_date . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="center">' . $key->receipt_number . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="left">' . $key->acct_name . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="left">' . $key->status . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="center">' . $key->YEAR . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="center">' . $key->MONTH . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->MIN . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->EXCESS . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . sprintf('%0.0f', $key->TOTAL) . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . sprintf('%0.2f', $key->pay_amount) . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . sprintf('%0.2f', $key->surchg) . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . sprintf('%0.2f', $key->lpaychg) . '</td></tr>';
		}
		$content = "
		<page>
        	<table style='width: 100%; padding-top:-10px;'>
	            <tr>
			        <td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '></td>
			        <td style='text-align: center;width=33%; padding-top:-5px;'>
						<h4>* Reference From *</h4>
					</td>
					<td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '></td>
				</tr>
				 <tr>
			        <td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '></td>
			        <td style='text-align: center;width=33%; padding-top:-5px;'>
						<h4>" . $_GET['rn_from'] . " to " . $_GET['rn_to'] . "</h4>
					</td>
					<td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '>     </td>
				</tr>
			</table><br><br>
			<table cellspacing='0' style='width: 100%; position: relative;'>
				<tr>
					<td style='border-bottom:1px solid; text-align: center;width=5%; padding: 5px;'>Date Pay</td>
					<td style='border-bottom:1px solid; text-align: center;width=10%; padding: 5px;'>Receipt Number</td>
					<td style='border-bottom:1px solid; text-align: center;width=14%; padding: 5px;'>Customer</td>
					<td style='border-bottom:1px solid; text-align: center;width=6%; padding: 5px;'>Type</td>
					<td style='border-bottom:1px solid; text-align: center;width=5%; padding: 5px;'>Year</td>
					<td style='border-bottom:1px solid; text-align: center;width=5%; padding: 5px;'>Month</td>
					<td style='border-bottom:1px solid; text-align: center;width=5%; padding: 5px;'>MIN</td>
					<td style='border-bottom:1px solid; text-align: center;width=6%; padding: 5px;'>EXS</td>
					<td style='border-bottom:1px solid; text-align: center;width=5%; padding: 5px;'>Total</td>
					<td style='border-bottom:1px solid; text-align: center;width=9%; padding: 5px;'>Pay Amount</td>
					<td style='border-bottom:1px solid; text-align: center;width=10%; padding: 5px;'>SURCHG 1/2</td>
					<td style='border-bottom:1px solid; text-align: center;width=10%; padding: 5px;'>Late Payment</td>
				</tr>" .
			$table_data
			. "</table>
		</page>
		";
		require_once dirname(__FILE__) . '\..\..\..\assets\html2pdf\html2pdf.class.php';
		$html2pdf = new HTML2PDF('P', 'A4', 'en');
		$html2pdf->pdf->SetTitle('Abstract Report');
		$html2pdf->WriteHTML($content);
		$html2pdf->Output('abstract_report_' . date("dmY_His") . '.pdf', 'D');
	}

	public function gen_excel() {

		$d1 = 'null';
		$d2 ='null';
		if (!empty($_GET['rn_from'])) {
			$d1 = $_GET['rn_from'];
		}
		if (!empty($_GET['rn_to'])) {
			$d2 = $_GET['rn_to'];
		}
		//activate worksheet number 1
		$excel = new PHPExcel();
		$excel->setActiveSheetIndex(0);
		//name the worksheet
		$excel->getActiveSheet()->setTitle('Transaction History');
		//set cell A1 content with some text
		$excel->getActiveSheet()->setCellValue('A1', 'Reference From '.$d1.' to '.$d2);
		$excel->getActiveSheet()->setCellValue('A3', 'Receipt Date');
		$excel->getActiveSheet()->setCellValue('B3', 'Receipt Number');
		$excel->getActiveSheet()->setCellValue('C3', 'Customer Name');
		$excel->getActiveSheet()->setCellValue('D3', 'Payment');
		$excel->getActiveSheet()->setCellValue('E3', 'Year');
		$excel->getActiveSheet()->setCellValue('F3', 'Month');
		$excel->getActiveSheet()->setCellValue('G3', 'Minimum');
		$excel->getActiveSheet()->setCellValue('H3', 'Excess');
		$excel->getActiveSheet()->setCellValue('I3', 'Total');
		$excel->getActiveSheet()->setCellValue('J3', 'Pay Amount');
		$excel->getActiveSheet()->setCellValue('K3', 'Half of Surcharge');
		$excel->getActiveSheet()->setCellValue('L3', 'Late Payment Charge');

		$result = $this->abstract_report_model->get_status($_GET['rn_from'],$_GET['rn_to']);
		$cellnum = 4;

		foreach ($result as $key) {
			$excel->getActiveSheet()->setCellValue('A' . $cellnum, $key->receipt_date);
			$excel->getActiveSheet()->setCellValue('B' . $cellnum, $key->receipt_number);
			$excel->getActiveSheet()->setCellValue('C' . $cellnum, $key->acct_name);
			$excel->getActiveSheet()->setCellValue('D' . $cellnum, $key->status);
			$excel->getActiveSheet()->setCellValue('E' . $cellnum, $key->YEAR);
			$excel->getActiveSheet()->setCellValue('F' . $cellnum, $key->MONTH);
			$excel->getActiveSheet()->setCellValue('G' . $cellnum, $key->MIN);
			$excel->getActiveSheet()->setCellValue('H' . $cellnum, $key->EXCESS);
			$excel->getActiveSheet()->setCellValue('I' . $cellnum, $key->TOTAL);
			$excel->getActiveSheet()->getStyle('I' . $cellnum)->getNumberFormat()->setFormatCode('###,###,###');
			$excel->getActiveSheet()->setCellValue('J' . $cellnum, $key->pay_amount);
			$excel->getActiveSheet()->getStyle('J' . $cellnum)->getNumberFormat()->setFormatCode('###,###,###.00');
			$excel->getActiveSheet()->setCellValue('K' . $cellnum, $key->surchg);
			$excel->getActiveSheet()->getStyle('K' . $cellnum)->getNumberFormat()->setFormatCode('###,###,###.00');
			$excel->getActiveSheet()->setCellValue('L' . $cellnum, $key->lpaychg);
			$excel->getActiveSheet()->getStyle('L' . $cellnum)->getNumberFormat()->setFormatCode('###,###,###.00');
			$cellnum++;
		}
		//$excel->getActiveSheet()->getStyle('A1:F' . $cellnum = $cellnum - 1)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

		// $excel->getActiveSheet()->getStyle('A1:F1')->getFill()->setFillType(PHPexcel_Style_Fill::FILL_SOLID);
		//       $excel->getActiveSheet()->getStyle('A1:F1')->getFill()->getStartColor()->setARGB('29bb04');
		//       // Add some data
		//       $excel->getActiveSheet()->getStyle("A1:F1")->getFont()->setBold(true);
		//       $excel->getActiveSheet()->getStyle('A1:F1')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getFill()->setFillType(PHPexcel_Style_Fill::FILL_SOLID);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getFill()->getStartColor()->setARGB('6F6F6F');
		// Add some data
		$excel->getActiveSheet()->getStyle("A3")->getFont()->setBold(true);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$excel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('C')->setAutoSize(false);
		$excel->getActiveSheet()->getColumnDimension('C')->setWidth(40);
		$excel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
		$excel->getActiveSheet()->getRowDimension('1')->setRowHeight(25);
		//change the font size
		$excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(12);
		//make the font become bold
		$excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		//merge cell A1 until D1
		$excel->getActiveSheet()->mergeCells('A1:F1');
		//set aligment to center for that merged cell (A1 to D1)
		$excel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		//$excel->getActiveSheet()->getStyle("A2:F2")->getFont()->getColor()->setRGB('FFFFFF');

		$excel->getActiveSheet()->setShowGridlines(false);

		$filename = 'abstract_report_ ' . date('Ymd_his') . '.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="' . $filename . '"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
		$objWriter->save('php://output');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("assets/tmp/" . $filename);
		//$this->index();
		// $data = ob_get_contents();
		//$tester =  $objWriter->save($filename);
		//force_download($filename,$objWriter->save('php://output')));
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */